from django.shortcuts import redirect


class login_middleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # 白名单
        whilelist = ['/admin/login/','/user/login/','/user/register/','/admin/']
        # 当前请求的路径
        path = request.path
        if path in whilelist or request.user.is_authenticated:
            response = self.get_response(request)
            return response
        else:
            return redirect('/user/login/')